set(PACKAGE_VERSION "@ARG_VERSION@")

if(@ARG_NAME@_DEBUG)
  message(STATUS "Debug mode on")
  message(STATUS "Installed @ARG_NAME@_VERSION: ${PACKAGE_VERSION}")

  message(STATUS "Installed @ARG_NAME@_BUILD_ARCHITECTURES: @standard_BUILD_ARCHITECTURES@")
  message(STATUS "Configured @ARG_NAME@_BUILD_ARCHITECTURES: ${@ARG_NAME@_BUILD_ARCHITECTURES}")

  message(STATUS "Installed CMAKE_SIZEOF_VOID_P: @CMAKE_SIZEOF_VOID_P@")
  message(STATUS "Configured CMAKE_SIZEOF_VOID_P: ${CMAKE_SIZEOF_VOID_P}")
endif()

# If the customer has an override architecture requirement, use that
if(DEFINED @ARG_NAME@_ARCHITECTURE)
  foreach(_arch IN LISTS @ARG_NAME@_ARCHITECTURE)
    if(_arch MATCHES "amd64|x86_64|x64")
      list(APPEND @ARG_NAME@_ARCHITECTURES x64)
    elseif(_arch MATCHES "i386|x86")
      list(APPEND @ARG_NAME@_ARCHITECTURES x86)
    elseif(_arch MATCHES "armeabi-v7a")
      list(APPEND @ARG_NAME@_ARCHITECTURES armeabi-v7a)
    elseif(_arch MATCHES "armeabi")
      list(APPEND @ARG_NAME@_ARCHITECTURES armeabi)
    elseif(_arch MATCHES "^arm")
      list(APPEND @ARG_NAME@_ARCHITECTURES arm)
    elseif(_arch MATCHES "arm64|aarch64")
      list(APPEND @ARG_NAME@_ARCHITECTURES aarch64)
    else()
      message(WARNING "Unrecognized architecture ${i}")
    endif()
  endforeach()
else()
  # Try to infer what the user wants
  if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
    set(@ARG_NAME@_ARCHITECTURES aarch64)
  elseif(CMAKE_SYSTEM_NAME MATCHES "Android")
    set(@ARG_NAME@_ARCHITECTURES "${CMAKE_ANDROID_ARCH_ABI}") #prevent mixing armeabi & armeabi-v7a
  elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
    set(@ARG_NAME@_ARCHITECTURES arm)
  elseif(CMAKE_SIZEOF_VOID_P EQUAL "8")
    set(@ARG_NAME@_ARCHITECTURES x64)
  else()
    set(@ARG_NAME@_ARCHITECTURES x86)
  endif()
endif()

if(@ARG_NAME@_DEBUG)
  message(STATUS "@ARG_NAME@_ARCHITECTURE: ${@ARG_NAME@_ARCHITECTURE}")
  message(STATUS "@ARG_NAME@_ARCHITECTURES: ${@ARG_NAME@_ARCHITECTURES}")
endif()

# Architectures we were built with
set(@ARG_NAME@_BUILD_ARCHITECTURES @standard_BUILD_ARCHITECTURES@)
if(@ARG_NAME@_DEBUG)
  message(STATUS "@ARG_NAME@_BUILD_ARCHITECTURES: ${@ARG_NAME@_BUILD_ARCHITECTURES}")
endif()

# Verify that we match all of the architectures requested by the customer
foreach(i IN LISTS @ARG_NAME@_ARCHITECTURES)
  list(FIND @ARG_NAME@_BUILD_ARCHITECTURES ${i} found)
  if(found EQUAL -1)
    set(PACKAGE_VERSION_COMPATIBLE FALSE)
    set(PACKAGE_VERSION_UNSUITABLE TRUE)
    if(@ARG_NAME@_DEBUG)
      message(STATUS "Requested architecture of ${@ARG_NAME@_ARCHITECTURE} not compatible with @standard_BUILD_ARCHITECTURES@")
    endif()
    return()
  endif()
endforeach()

# Determine whether the user's request (either implied or explicit) for libstdc++ can
# be met by this verison of @ARG_NAME@
if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
  if(@ARG_NAME@_DEBUG)
    message(STATUS "Installed @ARG_NAME@_USE_LIBCXX: @standard_USE_LIBCXX@")
    message(STATUS "Configured @ARG_NAME@_USE_LIBCXX: ${@ARG_NAME@_USE_LIBCXX}")
  endif()
endif()

if(NOT @ARG_VERSION_PRERELEASE@ STREQUAL "") # handle prerelease versions
  set(PACKAGE_FIND_VERSION_PRERELEASE "${@ARG_NAME@_FIND_VERSION_PRERELEASE}")
  if(NOT PACKAGE_FIND_VERSION_PRERELEASE)
    set(PACKAGE_VERSION_COMPATIBLE FALSE)
    message(AUTHOR_WARNING "To use this prerelease version of @ARG_NAME@, set @ARG_NAME@_FIND_VERSION_PRERELEASE to '@ARG_VERSION_PRERELEASE@'.")
  endif()

  if(NOT "${PACKAGE_FIND_VERSION}-${PACKAGE_FIND_VERSION_PRERELEASE}" STREQUAL "${PACKAGE_VERSION}")
    set(PACKAGE_VERSION_COMPATIBLE FALSE)
  else()
    set(PACKAGE_VERSION_EXACT TRUE)
  endif()
elseif(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
  if(@ARG_NAME@_DEBUG)
    message(STATUS "Our version is less than the requested one, trivially incompatible")
  endif()
  set(PACKAGE_VERSION_COMPATIBLE FALSE)
elseif(NOT PACKAGE_FIND_VERSION_MINOR EQUAL "@ARG_VERSION_MINOR@")
  if(@ARG_NAME@_DEBUG)
    message(STATUS "Minor version mismatch, requested minor is ${PACKAGE_FIND_VERSION_MINOR}, this version is ${PACKAGE_VERSION_MINOR}")
  endif()
  set(PACKAGE_VERSION_COMPATIBLE FALSE)
elseif(NOT PACKAGE_FIND_VERSION_MAJOR EQUAL "@ARG_VERSION_MAJOR@")
  if(@ARG_NAME@_DEBUG)
    message(STATUS "Major version mismatch, failure.")
  endif()
  set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
  set(PACKAGE_VERSION_COMPATIBLE TRUE)
  if (PACKAGE_VERSION VERSION_EQUAL PACKAGE_FIND_VERSION)
    set(PACKAGE_VERSION_EXACT TRUE)
  endif()

  if(@ARG_NAME@_DEBUG)
    message(STATUS "Using @ARG_NAME@ in ${CMAKE_CURRENT_SOURCE_DIR}")
  endif()
  set(@ARG_NAME@_VERSION_PRERELEASE "@ARG_VERSION_PRERELEASE@")
endif()
